import time
import unittest
import requests
import json
#测试登录模块
from parameterized import parameterized

class TestLogin(unittest.TestCase):
    #每一个test开始都会先执行
    def setUp(self):
        # 实例化session对象
        self.session=requests.Session()
        # 定义验证接口url地址
        self.verify_url="http://192.168.10.111:8681/verifycode?noPrefix=true"
        #定义正如接口url地址
        self.login_url="http://192.168.10.111:8681/api/auth/login"

    #每一个test结束都会执行
    def tearDown(self):
        self.session.close()

    def test01_login_success(self):
        #获取验证码
        response=self.session.get(self.verify_url)
        print("type=",response.headers.get("Content-type"))
        self.assertIn("image",response.headers.get("Content-type"))
        self.assertEqual(200,response.status_code)

        data = {"username": "rowen",
                "password": "xjt123",
                "verifyCode": "9999"}
        response=self.session.post(self.login_url,data=data)
        json_data=response.json()
        print("login response data1=",json_data)
        #断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(0,json_data.get("code"))
        self.assertEqual("操作成功",json_data.get("msg"))


    def test02_user_is_not_exist(self):
        #获取验证码
        response=self.session.get(self.verify_url)
        print("type=",response.headers.get("Content-type"))
        self.assertIn("image",response.headers.get("Content-type"))
        self.assertEqual(200,response.status_code)

        data = {"username": "jacke",
                    "password": "xjt123",
                    "verifyCode": "9999"}

        response=self.session.post(self.login_url,data=data)
        json_data=response.json()
        print("login response data1=",json_data)
        #断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(5901,json_data.get("code"))
        self.assertEqual("用户名或密码输入有误",json_data.get("msg"))

    def test03_password_error(self):
        #获取验证码
        response=self.session.get(self.verify_url)
        print("type=",response.headers.get("Content-type"))
        self.assertIn("image",response.headers.get("Content-type"))
        self.assertEqual(200,response.status_code)

        data = {"username": "jacke",
                "password": "xjt345",
                "verifyCode": "9999"}
        response=self.session.post(self.login_url,data=data)
        json_data=response.json()
        print("login response data1=",json_data)
        #断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(5901,json_data.get("code"))
        self.assertEqual("用户名或密码输入有误",json_data.get("msg"))

#
# if __name__=='__main__':
#     unittest.main()
#     suite = unittest.TestSuite()
#     suite.addTest(TestLogin('test_login_success'))
#     suite.addTest(TestLogin('test_login_username_is_not_exsit'))
#     suite.addTest(TestLogin('test_login_pwd_is_error'))
#     runner = unittest.TextTestRunner()
#     runner.run(suite)
#     # 这里 addTest()方法没有起到作用，还是按照原来的顺序执行了。原因是：在pycharm中，引入了unittest 模块，会默认按照 unittest
#     # 模式执行。需要将unittest模式转换成普通模式。
#     # 创建TextTestRunner类的实例对象
#
#
#     # # 测试报告文件路径
#     # report_file = "./report/report{}.html".format(time.strftime("%Y%m%d-%H%M%S"))
#     # with open(report_file, "wb") as f:
#     #     runner = HTMLTestRunner(f, title="TPshop接口自动化测试报告", description="V1.0")
#     #     # 运行测试套件
#     #     runner.run(suite)